package com.egg.ylt.presenter.impl;

import android.text.TextUtils;

import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.heartbeat.HeartbeatManageSingleton;
import com.egg.ylt.pojo.CurrentBabyInfoEntity;
import com.egg.ylt.pojo.RealtimeDataEntity;
import com.egg.ylt.pojo.SwimHistoryEntity;
import com.egg.ylt.presenter.ISwimPresenter;
import com.egg.ylt.view.ISwimView;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.mvp.impl.BasePresenter;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.util.HashMap;

/**
 * author:        yupeng
 * created on:    2019/2/22 14:44
 * description:   健康/游泳Presenter
 */
public class SwimPresenterImpl extends BasePresenter<ISwimView> implements ISwimPresenter {
    private CurrentBabyInfoEntity currentBabyInfoEntity;

    public void onInit() {

        getCurrentBaby(Constants.TOKEN, Constants.COMPANYID, Constants.BABYID);
        if (!TextUtils.isEmpty(Constants.SWIMRINGCODE)) {
            mView.showSwimRingConncetSuccess(Constants.SWIMRINGCODE);
        }
    }

    /**
     * 用户是否登录
     * @return
     */
    public boolean isUserLogin() {
        return !TextUtils.isEmpty(Constants.TOKEN);
    }

    public CurrentBabyInfoEntity getCurrentBabyModel() {
        return currentBabyInfoEntity;
    }

    public boolean hasAddedBaby() {
        return currentBabyInfoEntity != null;
    }

    @Override
    public void getCurrentBaby(String token, String companyId, String babyId) {
        HashMap<String, String> parms = new HashMap<>();
        parms.put("token", token);
        parms.put("companyId", companyId);
        parms.put("babyId", babyId);
        RequestManager.getInstance().requestPostByAsyn(API.GET_CURRENT_BABY_INFO, parms, new ReqCallBack<CurrentBabyInfoEntity>() {

            @Override
            public void onReqSuccess(CurrentBabyInfoEntity result) {
                currentBabyInfoEntity = result;
                mView.initializeBabyInfo();
            }

            @Override
            public void onFailure(String result) {
                mView.initializeBabyInfo();
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.initializeBabyInfo();
            }
        });

    }

    @Override
    public void getBabySwimData(String babyId) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("babyId", babyId);
        RequestManager.getInstance().requestGetByAsyn(API.GET_BABY_LIFE_EQUIPMENT_DATA, hashMap, new ReqCallBack<SwimHistoryEntity>() {
            @Override
            public void onReqSuccess(SwimHistoryEntity result) {
                mView.showBabySwimData(result);
            }

            @Override
            public void onFailure(String result) {
            }

            @Override
            public void onReqFailed(ErrorBean error) {
            }
        });
    }

    public void getShopRealTimeDataByHeartbeat(String shopId) {
        try {
            HeartbeatManageSingleton.getInstance.startAirWaterHeartbeat(new HeartbeatManageSingleton.OnHandleAirWaterEventCallback() {
                @Override
                public void onHandleAirWaterEvent(RealtimeDataEntity result) {
                    mView.showShopRealTimeData(result);
                }
            }, shopId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void getSteelyardLogin(String code, String token, String babyId) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", token);
        hashMap.put("code", code);
        hashMap.put("babyId", babyId);
        RequestManager.getInstance().requestPostByAsyn(API.SCAN_CODE_LOGIN, hashMap, new ReqCallBack<Object>() {
            @Override
            public void onReqSuccess(Object result) {
                mView.showSteelyardConnectSucess();
            }

            @Override
            public void onFailure(String result) {
                mView.showToast("连接抚触台失败：" + result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.showToast("连接抚触台失败：" + error);
            }
        });
    }

    @Override
    public void getSwimRingBind(String token, final String equipCode, String companyId, String babyId) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", token);
        hashMap.put("equipCode", equipCode);
        hashMap.put("companyId", companyId);
        hashMap.put("babyId", babyId);
        RequestManager.getInstance().requestPostByAsyn(API.POST_BIND_EQUIPMENT, hashMap, new ReqCallBack<Object>() {
            @Override
            public void onReqSuccess(Object result) {
                mView.showSwimRingConncetSuccess(equipCode);
            }

            @Override
            public void onFailure(String result) {
                mView.showToast("连接游泳圈失败：" + result);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.showToast("连接游泳圈失败：" + error);
            }
        });
    }
}
